package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import io.undertow.util.BadRequestException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {




    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }
        @Resource
        private WorkerCertificationAuditMapper auditMapper;

    @Override
    public PageResult<WorkerCertificationAuditResDTO> Page(WorkerCertificationAuditPageQueryReqDTO dto) {
        IPage page = new Page(dto.getPageNo(), dto.getPageSize());
        LambdaQueryWrapper<WorkerCertificationAudit> wrapper = Wrappers.<WorkerCertificationAudit>lambdaQuery()
                .eq(StringUtils.isNotBlank(dto.getIdCardNo()), WorkerCertificationAudit::getIdCardNo, dto.getIdCardNo())
                .eq(dto.getAuditStatus() != null, WorkerCertificationAudit::getAuditStatus, dto.getAuditStatus())
                .eq(dto.getCertificationStatus() != null, WorkerCertificationAudit::getCertificationStatus, dto.getCertificationStatus())
                .like(ObjectUtil.isNotEmpty(dto.getName()), WorkerCertificationAudit::getName, dto.getName())
                .orderBy(StringUtils.isNotBlank(dto.getOrderBy1()), dto.getIsAsc1(), WorkerCertificationAudit::getCreateTime);
        IPage iPage = auditMapper.selectPage(page, wrapper);
        PageResult<WorkerCertificationAuditResDTO> pageResult=new PageResult<>();
        pageResult.setPages(iPage.getPages());
        pageResult.setTotal(iPage.getTotal());
        pageResult.setList(iPage.getRecords());
        return pageResult;
    }
    @Resource
    private WorkerCertificationMapper workerCertificationMapper;
    @Override
    public void auditcertificationrenyuan(Long id, Integer certificationStatus, String rejectReason) throws BadRequestException {
        WorkerCertificationAudit workerCertificationAudit = auditMapper.selectById(id);
        if(workerCertificationAudit==null){
            throw new BadRequestException("审核失败，未找到该审核记录");
        }
        if(StringUtils.isNotBlank(rejectReason)){
            workerCertificationAudit.setRejectReason(rejectReason);
        }
        workerCertificationAudit.setCertificationStatus(certificationStatus);
        workerCertificationAudit.setAuditStatus(1);
        workerCertificationAudit.setAuditorId(UserContext.currentUserId());
        workerCertificationAudit.setAuditorName(UserContext.currentUser().getName());
        workerCertificationAudit.setAuditTime(LocalDateTime.now());
        auditMapper.updateById(workerCertificationAudit);
        LambdaUpdateWrapper<WorkerCertification> wrapper = Wrappers.<WorkerCertification>lambdaUpdate().set(WorkerCertification::getCertificationStatus,
                certificationStatus).eq(WorkerCertification::getId, workerCertificationAudit.getServeProviderId());
        workerCertificationMapper.update(new WorkerCertification(),wrapper);
    }
    }

